<?php
function getSubMenu($menus,$parentID){
	$items = array();
	foreach($menus as $menu){
		if($menu['parentID'] == $parentID){
			$items[] = $menu;
		}
	}
	return $items;
}
function menuOption($menus,$item){
	$str = "<tr class='bg'>";
	$str .= "<td width='100'>{$item['id']}</td>";
    $str .= "<td style='text-align:left;' width='200'>".str_repeat('&mdash;',substr_count($item['path'],',')-1).$item['title']."</td>";
    $str .= "<td width='200'>".mb_substr($item['url'],0,14,'utf8')."</td>";
    $str .= "<td width='100'>{$item['sortID']}</td>";
	$str .= "<td class='odd' width='100'>";
	$str .= "<a class='ajax' id='{$item['id']}' href='".url('menu/edit?id=').$item['id']."'>编辑</a>|";
    $str .= "<a class='del' id='{$item['id']}' href='javascript:void(0);' >删除</a></td></tr>";
	echo $str;
	$children = getSubMenu($menus,$item['id']);
	if(!empty($children)){
		foreach($children as $child){
			menuOption($menus,$child);
		}
	}
}

?>
<div class="list">
	<span class="txt"><a class="ajax2" href="<?php echo url('menu/add');?>"></a></span>
    <table cellspacing="0" cellpadding="0" style="text-align:left;">
	<tr class="th">
            <th>ID</th>	
            <th>菜单名称</th>
            <th>链接</th>
            <th>排序ID</th>
            <th>操作</th>
	</tr>
    <?php 
		foreach($topMenus as $item){
			menuOption($menus,$item);
		}
	?>
    </table>
</div>

<script type="text/javascript">
$(document).ready(function(){
	$(".del").click(function(){
		if(confirm("确认删除")){
			that = $(this);
			$.post("<?php echo url('menu/del');?>",{id:that.attr('id')},function(data){
				if(data.s==1){
					that.parents('tr').remove();
				}else{
					alert(data.i);
				}
			});
		}
		return false;
		});	
});
</script>
